package com.shop2cn.iapi.sqoptrade.testcase;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

import org.junit.*;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.shenqisettlement.parameter.bean.AgentProductBean;
import com.shop2cn.iapi.sqoptrade.parameter.req.BatchChangeSqDeliveryInfoReq;
import com.shop2cn.iapi.sqoptrade.parameter.vo.ChangeSqDeliveryInfoVo;
import com.shop2cn.iapi.sqoptrade.resp.TradingServiceResp;
import com.shop2cn.iapi.sqoptrade.service.BatchChangeSqDeliveryInfoCall;
import com.ymatou.iapi.synctrade.service.TradingSyncService;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.SQPayGateWayCallService;
import com.ymttest.business.service.shenqi.DGSQBuildProxyChainCallService;
import com.ymttest.business.service.shenqi.SqTradingCallService;
import com.ymttest.database.sharding.db.ext.model.DeliveryPo;
import com.ymttest.database.sharding.db.sqlwapper.OrderWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;

public class Ts_SqBatchChangeDeliveryInfo {
	
	private static BatchChangeSqDeliveryInfoReq batchChangeSqDeliveryInfoReq;
	private static BatchChangeSqDeliveryInfoCall batchChangeSqDeliveryInfoCall;

	private static OrderWapper orderWapper = new OrderWapper();
	
	private static long autotest_goodsOwnerId = Long.valueOf(EnvSetup
			.getData("autotest_goodsOwnerId"));

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("修改神器发货物流信息");
	}

	@Before
	public void caseUp() {

		batchChangeSqDeliveryInfoReq = new BatchChangeSqDeliveryInfoReq();
		batchChangeSqDeliveryInfoCall = new BatchChangeSqDeliveryInfoCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(batchChangeSqDeliveryInfoCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	/**
	 * 校验 mysql delivery 表
	 * 
	 * @param infos
	 */
	private void verifyMysqlDb(List<ChangeSqDeliveryInfoVo> infos, long goodsOwnerId,
			int logisticsType) {

		long orderId = batchChangeSqDeliveryInfoReq.getOrderId();

		List<DeliveryPo> actualDeliveryPoList = orderWapper
				.selectDeliverysByOrderId(orderId);
		
		Logger.verifyEquals(infos.size(), actualDeliveryPoList.size(), "验证数据库size");
		
		for (int i = 0; i < actualDeliveryPoList.size(); i++) {
			
			Logger.comment("校验 delivery 表数据");
			
			DeliveryPo actualDeliveryPo = actualDeliveryPoList.get(i);
			ChangeSqDeliveryInfoVo info = infos.get(i);

			Logger.verifyEquals(info.getYmtExpressCode(),
					actualDeliveryPo.getYmtExpressCode(), "验证数据库YmtExpressCode");

			Logger.verifyEquals(false, actualDeliveryPo.getOfficalLogistics(),
					"验证数据库OfficalLogistics");

			Logger.verifyEquals(orderId, actualDeliveryPo.getOrderId(),
					"验证数据库OrderId");

			Logger.verifyEquals(goodsOwnerId, actualDeliveryPo.getSellerId(),
					"验证数据库SellerId");

			Logger.verifyEquals(logisticsType, actualDeliveryPo.getLogisticsType(),
					"验证数据库LogisticsType");

			Logger.verifyEquals(false, actualDeliveryPo.getDeleted(),
					"验证数据库Deleted");

			Logger.verifyEquals(info.getBillNo(), actualDeliveryPo.getBillNo(),
					"验证数据库BillNo");
			
			Logger.verifyEquals(info.getYmtExpressCode(), actualDeliveryPo.getYmtExpressCode(),
					"验证数据库YmtExpressCode");
			
			Logger.verifyEquals(info.getYmtExpressName(), actualDeliveryPo.getExpressName(),
					"验证数据库ExpressName");
			
			Logger.verifyEquals(logisticsType == 100 ? 1 : 3, actualDeliveryPo.getSection(),
					"验证数据库Section");
			
			Logger.verifyEquals(logisticsType, actualDeliveryPo.getLogisticsType(),
					"验证数据库LogisticsType");
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	@Ignore
	public void Tc_BatchDelivery_001() {
		Logger.start(true, "修改发货信息成功");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false);
			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrder2(BigDecimal.ZERO, productBean, 1);

			Long userId = placeOrderResp.getUserId();
			long goodsOwnerId = placeOrderResp.getOrderList().get(0)
					.getOrderPo().getGoodsOwnerId();
			long orderId = placeOrderResp.getOrderList().get(1).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(3000);

			// 接单
			SqTradingCallService.sellerAcceptOrder(goodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);

			Thread.sleep(5000);

			// 神器订单发货
			String oldBillNo = SqTradingCallService.delivery(autotest_goodsOwnerId, orderId,
					1L, null);
			TradingSyncService.getSyncResult(orderId);

			Logger.comment("前置数据准备结束");
			
			ChangeSqDeliveryInfoVo changeSqDeliveryInfoVo = new ChangeSqDeliveryInfoVo();
			
			changeSqDeliveryInfoVo.setBillNo(TestDataManager.getRandomNum(13));
			changeSqDeliveryInfoVo.setYmtExpressCode("Y037");
			changeSqDeliveryInfoVo.setYmtExpressName("圆通速递-国际件");
			changeSqDeliveryInfoVo.setOldBillNo(oldBillNo);
			changeSqDeliveryInfoVo.setOldYmtExpressCode("Y013");
			changeSqDeliveryInfoVo.setOldYmtExpressName("sf");
			
			// 准备参数
			batchChangeSqDeliveryInfoReq.setAppId("autotest");
			batchChangeSqDeliveryInfoReq.setGoodsOwnerId(goodsOwnerId);
			batchChangeSqDeliveryInfoReq.setOrderId(orderId);
			batchChangeSqDeliveryInfoReq.setChangeSqDeliveryInfoVos(Arrays.asList(changeSqDeliveryInfoVo));
			
			batchChangeSqDeliveryInfoCall.setData(batchChangeSqDeliveryInfoReq);
			batchChangeSqDeliveryInfoCall.callService();
			
			verifyMysqlDb(
					batchChangeSqDeliveryInfoReq.getChangeSqDeliveryInfoVos(),
					autotest_goodsOwnerId, 100);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_BatchDelivery_002() {
		Logger.start(true, "输入错误的旧物流信息，提示物流信息不存在");
		try {
			
			ChangeSqDeliveryInfoVo changeSqDeliveryInfoVo = new ChangeSqDeliveryInfoVo();
			
			changeSqDeliveryInfoVo.setBillNo(TestDataManager.getRandomNum(13));
			changeSqDeliveryInfoVo.setYmtExpressCode("Y037");
			changeSqDeliveryInfoVo.setYmtExpressName("圆通速递-国际件");
			changeSqDeliveryInfoVo.setOldBillNo("1222239490708");
			changeSqDeliveryInfoVo.setOldYmtExpressCode("Y013");
			changeSqDeliveryInfoVo.setOldYmtExpressName("sf");
			
			// 准备参数
			batchChangeSqDeliveryInfoReq.setAppId("autotest");
			batchChangeSqDeliveryInfoReq.setGoodsOwnerId(500002490L);
			batchChangeSqDeliveryInfoReq.setOrderId(6725048699L);
			batchChangeSqDeliveryInfoReq.setChangeSqDeliveryInfoVos(Arrays.asList(changeSqDeliveryInfoVo));
			
			batchChangeSqDeliveryInfoCall.setData(batchChangeSqDeliveryInfoReq);
			batchChangeSqDeliveryInfoCall.callService();
			
			Logger.verifyEquals("原物流信息不存在", batchChangeSqDeliveryInfoCall.getMsg(),
					"验证errorMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	@Ignore
	public void Tc_BatchDelivery_003() {
		Logger.start(true, "修改发货信息并添加新的发货信息成功");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false);
			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrder(productBean);

			Long userId = placeOrderResp.getUserId();
			long goodsOwnerId = placeOrderResp.getOrderList().get(0)
					.getOrderPo().getGoodsOwnerId();
			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(3000);

			// 接单
			SqTradingCallService.sellerAcceptOrder(goodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);

			Thread.sleep(5000);

			// 神器订单发货
			String oldBillNo = SqTradingCallService.delivery(autotest_goodsOwnerId, orderId,
					1L, null);
			TradingSyncService.getSyncResult(orderId);

			Logger.comment("前置数据准备结束");
			
			ChangeSqDeliveryInfoVo changeSqDeliveryInfoVo1 = new ChangeSqDeliveryInfoVo();
			
			changeSqDeliveryInfoVo1.setBillNo(TestDataManager.getRandomNum(13));
			changeSqDeliveryInfoVo1.setYmtExpressCode("Y037");
			changeSqDeliveryInfoVo1.setYmtExpressName("圆通速递-国际件");
			changeSqDeliveryInfoVo1.setOldBillNo(oldBillNo);
			changeSqDeliveryInfoVo1.setOldYmtExpressCode("Y013");
			changeSqDeliveryInfoVo1.setOldYmtExpressName("sf");
			
			ChangeSqDeliveryInfoVo changeSqDeliveryInfoVo2 = new ChangeSqDeliveryInfoVo();
			
			changeSqDeliveryInfoVo2.setBillNo(TestDataManager.getRandomNum(13));
			changeSqDeliveryInfoVo2.setYmtExpressCode("Y037");
			changeSqDeliveryInfoVo2.setYmtExpressName("圆通速递-国际件");
			
			// 准备参数
			batchChangeSqDeliveryInfoReq.setAppId("autotest");
			batchChangeSqDeliveryInfoReq.setGoodsOwnerId(goodsOwnerId);
			batchChangeSqDeliveryInfoReq.setOrderId(orderId);
			batchChangeSqDeliveryInfoReq.setChangeSqDeliveryInfoVos(Arrays
					.asList(changeSqDeliveryInfoVo1, changeSqDeliveryInfoVo2));
			
			batchChangeSqDeliveryInfoCall.setData(batchChangeSqDeliveryInfoReq);
			batchChangeSqDeliveryInfoCall.callService();
			
			verifyMysqlDb(
					batchChangeSqDeliveryInfoReq.getChangeSqDeliveryInfoVos(),
					autotest_goodsOwnerId, 999);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_BatchDelivery_004() {
		Logger.start(true, "不输入物流信息BillNo，提示发货错误");
		try {
			
			ChangeSqDeliveryInfoVo changeSqDeliveryInfoVo = new ChangeSqDeliveryInfoVo();
			
			//changeSqDeliveryInfoVo.setBillNo(TestDataManager.getRandomNum(13));
			changeSqDeliveryInfoVo.setYmtExpressCode("Y037");
			changeSqDeliveryInfoVo.setYmtExpressName("圆通速递-国际件");
			changeSqDeliveryInfoVo.setOldBillNo("1222239490708");
			changeSqDeliveryInfoVo.setOldYmtExpressCode("Y013");
			changeSqDeliveryInfoVo.setOldYmtExpressName("sf");
			
			// 准备参数
			batchChangeSqDeliveryInfoReq.setAppId("autotest");
			batchChangeSqDeliveryInfoReq.setGoodsOwnerId(500002490L);
			batchChangeSqDeliveryInfoReq.setOrderId(6725048699L);
			batchChangeSqDeliveryInfoReq.setChangeSqDeliveryInfoVos(Arrays.asList(changeSqDeliveryInfoVo));
			
			batchChangeSqDeliveryInfoCall.setData(batchChangeSqDeliveryInfoReq);
			batchChangeSqDeliveryInfoCall.callService();
			
			Logger.verifyEquals("changeSqDeliveryInfoVos[0].billNo:面单号未提供", batchChangeSqDeliveryInfoCall.getMsg(),
					"验证errorMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_BatchDelivery_005() {
		Logger.start(true, "不输入旧物流信息，直接新增一个物流信息");
		try {
		
			ChangeSqDeliveryInfoVo changeSqDeliveryInfoVo = new ChangeSqDeliveryInfoVo();

			changeSqDeliveryInfoVo.setBillNo(TestDataManager.getRandomNum(13));
			changeSqDeliveryInfoVo.setYmtExpressCode("Y037");
			changeSqDeliveryInfoVo.setYmtExpressName("圆通速递-国际件");

			// 准备参数
			batchChangeSqDeliveryInfoReq.setAppId("autotest");
			batchChangeSqDeliveryInfoReq.setGoodsOwnerId(500002490L);
			batchChangeSqDeliveryInfoReq.setOrderId(6738248699L);
			batchChangeSqDeliveryInfoReq.setChangeSqDeliveryInfoVos(Arrays
					.asList(changeSqDeliveryInfoVo));

			batchChangeSqDeliveryInfoCall.setData(batchChangeSqDeliveryInfoReq);
			batchChangeSqDeliveryInfoCall.callService();
			
			DeliveryPo actualDeliveryPo = orderWapper
					.selectDeliveryByOrderId(6738248699L);
			
			Logger.comment("校验 delivery 表数据");
			

			Logger.verifyEquals(changeSqDeliveryInfoVo.getYmtExpressCode(),
					actualDeliveryPo.getYmtExpressCode(), "验证数据库YmtExpressCode");

			Logger.verifyEquals(false, actualDeliveryPo.getOfficalLogistics(),
					"验证数据库OfficalLogistics");

			Logger.verifyEquals(6738248699L, actualDeliveryPo.getOrderId(),
					"验证数据库OrderId");

			Logger.verifyEquals(500002490L, actualDeliveryPo.getSellerId(),
					"验证数据库SellerId");

			Logger.verifyEquals(100, actualDeliveryPo.getLogisticsType(),
					"验证数据库LogisticsType");

			Logger.verifyEquals(false, actualDeliveryPo.getDeleted(),
					"验证数据库Deleted");

			Logger.verifyEquals(changeSqDeliveryInfoVo.getBillNo(), actualDeliveryPo.getBillNo(),
					"验证数据库BillNo");
			
			Logger.verifyEquals(changeSqDeliveryInfoVo.getYmtExpressCode(), actualDeliveryPo.getYmtExpressCode(),
					"验证数据库YmtExpressCode");
			
			Logger.verifyEquals(changeSqDeliveryInfoVo.getYmtExpressName(), actualDeliveryPo.getExpressName(),
					"验证数据库ExpressName");
			
			Logger.verifyEquals(1, actualDeliveryPo.getSection(),
					"验证数据库Section");
			
			Logger.verifyEquals(100, actualDeliveryPo.getLogisticsType(),
					"验证数据库LogisticsType");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
